/*
 * poset1.java
 *
 * Created on 17 marzec 2007, 16:44
 */

package formykwadratowe;

import java.util.HashMap;
import java.util.HashSet;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author  zurekm
 */

public class poset1 extends javax.swing.JFrame {
    
    /** Creates new form poset1 */
    public poset1() {
        
        initComponents();
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("wygeneruj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("WPROWADZ POSET");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        HashMap<String, Node> graph = new HashMap<String, Node>();
        Node node;
        String poset;
        //jTextArea1.setText("1 | 3\n2 | 4\n3 | 5\n4 | 5,6\n5 | *,+\n6 | +,A\n7 | 6,8\n8 | A") ;
        poset=jTextArea1.getText();
        String[] sNodes = poset.split("[\n]");
        for (String sNode : sNodes ) {
            String[] children = sNode.split("[|,]");
            String nodeName = children[0].trim();
            if (graph.containsKey(nodeName)) {
                node = graph.get(nodeName);
            } else {
                node = new Node(nodeName);
                graph.put(nodeName, node);
            }
            Node targetNode;
            for (int i=1; i<children.length; i++) {
                String targetName = children[i].trim();
                if (graph.containsKey(targetName)) {
                    targetNode = graph.get(targetName);
                } else {
                    targetNode = new Node(targetName);
                }
                targetNode.addDown(nodeName);
                node.addUp(targetName);
                graph.put(targetName, targetNode);
            }
            graph.put(nodeName, node);
            
        }
        
        String wynik_kwadraty = "";
        String wynik_plusy = "";
        String wynik_minusy = "";
        
        SortedSet<String> set = new TreeSet<String>(graph.keySet());
        oknoGlowne.rozmiarMacierzy = set.size();
        int i = oknoGlowne.rozmiarMacierzy;
        int k=i;
        oknoGlowne.wspolczynniki = new float[i][i];
        for(int n=0;n<i;n++ ){
            oknoGlowne.wspolczynniki[n][n]=1;
        }
        
        for (String name: set){
            
            boolean finished = false;
            
            HashSet<String> visiting = new HashSet<String>(graph.get(name).getUp());
            HashSet<String> destinations = new HashSet<String>(graph.get(name).getUp());
            HashSet<String> temp;
            if ( visiting.isEmpty() ) {
                finished = true;
            }
            while (!finished) {
                temp = new HashSet<String>(0);
                for (String key : visiting) {
                    node = graph.get(key);
                    destinations.addAll(node.getUp());
                    temp.addAll(node.getUp());
                }
                finished = temp.isEmpty();
                temp.removeAll(visiting);
                visiting = temp;
            }
            
            wynik_kwadraty += "+" + name + "^2 ";
            
            int nameInt,endInt;

            for (String end : destinations) {
                if (graph.get(end).isMaxElement()) {
                    wynik_minusy += " - (" + name + "" + end + ")";
                    try{
                        nameInt = Integer.valueOf( name ).intValue();
                        oknoGlowne.wspolczynniki[nameInt-1][k-1]=-(float)1/2;
                           oknoGlowne.wspolczynniki[k-1][nameInt-1]=-(float)1/2;
                    }catch (NumberFormatException nfex){
                    }
                } else {
                    wynik_plusy += " + (" + name + "" + end + ")";
                    try{
                        nameInt = Integer.valueOf( name ).intValue();
                        endInt = Integer.valueOf( end ).intValue();
                        oknoGlowne.wspolczynniki[nameInt-1][endInt-1] = (float)1/2;
                         oknoGlowne.wspolczynniki[endInt-1][nameInt-1] = (float)1/2;
                    }catch (NumberFormatException nfex){
                    }
                }
            }
            
            
            
            //wersja oryginalna
//            for (String end : destinations) {
//                if (graph.get(end).isMaxElement()) {
//                    wynik_minusy += " - (" + name + "" + end + ")";
//                } else {
//                    wynik_plusy += " + (" + name + "" + end + ")";
//                }
//            }
                
            }
            oknoGlowne.poleWynikow.setText(wynik_kwadraty + wynik_plusy + wynik_minusy+ "\n");
           
            this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed
        
       
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
    
    }
